/**
 * 初始化job详情对话框
 */
var JobInfoDlg = {

		jobInfoData : {},
		validateFields: {
			jobName: {
				validators: {
					notEmpty: {
						message: '任务名称不能为空'
					}
				}
			},
			cron: {
				validators: {
					notEmpty: {
						message: 'cron表达式不合法'
					}
				}
			},
			springBeanName: {
				validators: {
					notEmpty: {
						message: '类名不能为空'
					}
				}
			},
			methodName: {
				validators: {
					notEmpty: {
						message: '方法名不能为空'
					}
				}
			},
			status: {
				validators: {
					notEmpty: {
						message: '状态不能为空'
					}
				}
			}


		}
};


/**
 * 清除数据
 */
JobInfoDlg.clearData = function() {
	this.jobInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
JobInfoDlg.set = function(key, val) {
	this.jobInfoData[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
	return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
JobInfoDlg.get = function(key) {
	return $("#" + key).val();
}
/**
 * 收集数据
 */
JobInfoDlg.collectData = function() {
	this.set('id').set('jobName').set('springBeanName').set('methodName').set('cron').set('status').set('description').set('remarks').set('type');
}

/**
 * 关闭此对话框
 */
JobInfoDlg.close = function() {
	parent.layer.close(window.parent.job.layerIndex);
}


/**
 * 验证数据是否为空
 */
JobInfoDlg.validate = function () {
	$('#jobInfoForm').data("bootstrapValidator").resetForm();
	$('#jobInfoForm').bootstrapValidator('validate');
	return $("#jobInfoForm").data('bootstrapValidator').isValid();
}

/**
 * 提交添加
 */
JobInfoDlg.addSubmit = function() {

	this.clearData();
	this.collectData();

	if (!this.validate()) {

		return;
	}


	//提交信息
	var ajax = new $ax("/job/save", function(data){
		EA.success("添加成功!");
		window.parent.job.table.refresh();
		JobInfoDlg.close();
	},function(data){
		EA.error("添加失败!" + data.responseJSON.message + "!");
	});
	ajax.set(JobInfoDlg.jobInfoData);

	ajax.start();
}

/**
 * 提交修改
 */
JobInfoDlg.editSubmit = function() {

	this.clearData();
	this.collectData();
	if (!this.validate()) {

		return;
	}

	//提交信息
	var ajax = new $ax("/job/save", function(data){
		EA.success("修改成功!");
		window.parent.job.table.refresh();
		JobInfoDlg.close();
	},function(data){
		EA.error("修改失败!" + data.responseJSON.message + "!");
	});
	ajax.set(JobInfoDlg.jobInfoData);
	ajax.start();
}

JobInfoDlg.showMethods =function() {
	$("#methodName").val('');
	var val = $("#springBeanName").val();
	if (val == "") {
		return;
	}

	//提交信息
	var ajax = new $ax('/job/beans/' + val, function(data){
		var select = $("#methodName");
		select.empty();
		if (data.length == 0) {
			return;
		}

		for (var i = 0; i < data.length; i++) {
			var v = data[i];
			
			var m=$("#mname").val();
			if(m!=null){
				if(v==m){
					select.append("<option value='" + v + "' selected>" + v + "</option>")
				}else{
					select.append("<option value='" + v + "'>" + v + "</option>")
				}
			}else{
				select.append("<option value='" + v + "'>" + v + "</option>")
			}
			
		}
	},function(data){

	});

	ajax.start();


}

JobInfoDlg.initBeanNames=function() {
	var ajax = new $ax('/job/beans', function(data){
		var select = $("#springBeanName");
		for (var i = 0; i < data.length; i++) {
			var v = data[i];
			var m=$("#beanname").val();
			if(m!=null){
			
				if(v==m){
					select.append("<option value='" + v + "' selected>" + v + "</option>")
				}else{
					select.append("<option value='" + v + "'>" + v + "</option>")
				}
			}else{
				select.append("<option value='" + v + "'>" + v + "</option>")
			}
		
		}
	},function(data){

	});

	ajax.start();

}

JobInfoDlg.checkCorn =function() {
	var val = $("#cron").val();
	if (val == "") {
		return;
	}

	//提交信息
	var ajax = new $ax('/job/cron?cron=' + val, function(data){

		if(!data){
			EA.error("cron表达式不合法!");
			$('#cron').val('');
		}
	},function(data){

	});

	ajax.start();


}

$(function() {
	EA.initValidator("jobInfoForm", JobInfoDlg.validateFields);


	//框失去焦点
	$('#cron').blur(function() {

		JobInfoDlg.checkCorn();
	});

	JobInfoDlg.initBeanNames();	

	JobInfoDlg.showMethods();

});
